package com.ld.tool.controller.tool;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ld.admin.controller.AdminBasicController;
import com.ld.shieldsb.common.composition.util.web.Global;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.JsoupUtil;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;
import com.ld.tool.model.tool.ShieldjsDemoModel;
import com.ld.tool.service.tool.ShieldjsDemoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * shieldjs示例
 * 
 * @ClassName ShieldjsDemoController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年06月14日 11:38:05
 *
 */
@Api(tags = "shieldjs示例")
@Controller(value = "toolShieldjsDemoController")
@RequestMapping("${shieldsb.adminPath}/tool/shieldjsdemo")
public class ShieldjsDemoController extends AdminBasicController<ShieldjsDemoModel> {
    @Autowired
    protected ShieldjsDemoService shieldjsDemoService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/tool/shieldjsdemo/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/tool/shieldjsdemo/shieldjsdemo_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = shieldjsDemoService.getSearchQueryModel(request);
        shieldjsDemoService.getPageBean(request, getModelClass(), queryModel);
        return "a/tool/shieldjsdemo/shieldjsdemo_list";
    }

    @ApiOperation(value = "shieldjs示例展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        shieldjsDemoService.getOne(request, getModelClass(), id);
        return "a/tool/shieldjsdemo/shieldjsdemo_show";
    }

    @ApiOperation(value = "跳转到添加/修改shieldjs示例", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        shieldjsDemoService.getOne(request, getModelClass(), id);
        return "a/tool/shieldjsdemo/shieldjsdemo_update";
    }

    @ApiOperation(value = "添加/修改shieldjs示例", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid ShieldjsDemoModel model) {
        Result result = shieldjsDemoService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除shieldjs示例", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = shieldjsDemoService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "获取示例信息", notes = "")
    @RequestMapping(path = "/getDemo", method = RequestMethod.GET)
    // 返回json格式
//    @ResponseBody
    public Object getDemo() throws ServletException, IOException {
        String name = getString("name").trim();
        // 读取配置文件
        String getColsUrl = PropertiesModel.CONFIG.getString("shieldjsdemoUrl");
        if (StringUtils.isBlank(getColsUrl)) {
            getColsUrl = Global.getProjectFrontURL() + "/tool/shieldjsdemo?name=";
        }
        if (StringUtils.isNotBlank(getColsUrl)) {
            JSONObject json = JsoupUtil.getRemoteURLJson(getColsUrl + name, 15);
            if (json != null) {
                request.setAttribute("model", json.get("data"));
            }
        }
//        return Web.Response.success("", colsModels);
        return "a/tool/shieldjsdemo/shieldjsdemo_info";
    }

}